<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 意见反馈
 *
 * @author www.dingwei.cn
 */

class AdmFeedbackModel extends MY_Model{
	
	private $tbFeedback = 'admin_feedback';
	
	/**
	 * 保存
	 * @param 	array 		$data
	 * @param 	integer 	$id
	 * @return	array
	 */
	public function save($data, $id = 0){
		if($id){
			$this->db->where('id', $id);
			$tA = $this->update($this->tbFeedback, $data);
		}else{
            $tA = $this->insert($this->tbFeedback, $data);
		}
		
		return $tA;
	}

    /**
     * 历史反馈意见
     * @param $consumerId  消费者id
     * @param $merchantId  商户id
     * @param $page
     * @return array
     * @khj
     */
    public function appGetFeedbackList($consumerId = 0, $merchantId = 0, $page){

        if( ! $merchantId && ! $consumerId){
            //不能同时为0
            return array();
        }

        if($consumerId){
            $this->db->where('consumer_id', $consumerId);
        }

        if($merchantId){
            $this->db->where('merchant_id', $merchantId);
        }

        $this->db->select('content,reply,status,addtime')
            ->order_by('addtime desc');

        $tA = $this->appPage($this->tbFeedback,$page);

        foreach ($tA['list'] as $key=>&$value){
            $value['addtime'] = dateFormat($value['addtime']);
            $value['reply'] = $value['reply']?$value['reply']:'';
        }

        return $tA;
    }

    /**
     * 后台获取列表
     * @param  array        $ci
     * @return  array
     */
	public function adminPcGetList($ci = array()){
	    $content = trim(get('content'));
	    $status = (int)get('status');
	    $type = (int)get('type');
        $send_date_from = trim(get('send_date_from'));
        $send_date_to = trim(get('send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

	    if ($content){
	        $this->db->like('fb.content', $content);
        }

        if ($status){
	        $this->db->where(array('fb.status' => $status));
        }
        if (strlen($send_date_from) && strlen($send_date_to)){
            $this->db->where(array('fb.addtime >= ' => strtotime($send_date_from), 'fb.addtime <= ' => strtotime($send_date_to)));
        }
        if($type == 1){//商家反馈
            $this->db->where(array('fb.merchant_id >' => 0, 'fb.status' => 1));
        }
        if($type == 2){//用户反馈
            $this->db->where(array('fb.consumer_id >' => 0, 'fb.status' => 1));
        }
        if (!empty($ci)){
            get_instance()->loadModel('MerchantUserModel', 'mMerchant', 'merchant');
            $this->db->join($this->mMerchant->tbUser.' mer', 'fb.merchant_id=mer.id')
                ->join($this->mMerchant->tbShop.' s', 's.merchant_id=mer.id')
                ->where_in('s.county', $ci)
                ->where(array('fb.merchant_id >' => 0));
        }

        $this->db->select('fb.id,fb.consumer_id,fb.merchant_id,,fb.content,fb.reply,fb.status,fb.addtime');

        if ($iSortCol_0 == 1){
            $this->db->order_by('fb.addtime '.$sSortDir_0);
        }else{
            $this->db->order_by('fb.id desc');
        }
        $data = $this->pagination($this->tbFeedback.' fb');
        if (!empty($data['data'])) {
            $cIds = array();
            $mIds = array();
            foreach ($data['data'] as $k => $v) {
                if (!in_array($v['consumer_id'], $cIds) && $v['consumer_id'] > 0) {
                    $cIds[] = $v['consumer_id'];
                }
                if (!in_array($v['merchant_id'], $mIds) && $v['merchant_id'] > 0) {
                    $mIds[] = $v['merchant_id'];
                }
            }
            get_instance()->loadModel('ConsumerUserModel', 'mCUser', 'consumer');
            get_instance()->loadModel('MerchantUserModel', 'mMUser', 'merchant');
            $consumer = $this->mCUser->getInfoByIds($cIds);
            $merchant = $this->mMUser->getInfoByIds($mIds);
            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]['consumer_name'] =  '';
                $data['data'][$k]['merchant_name'] =  '';
                if (isset($consumer[$v['consumer_id']]) && $v['consumer_id'] > 0){
                    $data['data'][$k]['consumer_name'] =  $consumer[$v['consumer_id']]['nickname'];
                }
                if (isset($merchant[$v['merchant_id']]) && $v['merchant_id'] > 0){
                    $data['data'][$k]['merchant_name'] =  $merchant[$v['merchant_id']]['shop_name'];
                }
                $data['data'][$k]['addtime'] = dateFormat($v['addtime'], true);
            }
        }
        return $data;
    }

    /**
     * 删除
     * @param $id           意见反馈id
     * @return array|bool
     */
    public function deleteFeedback($id){
        if (!$id) return array();
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbFeedback );
    }

    /**
     * 获取未处理的意见反馈数量
     * @return array
     */
    public function getFeedbackCount($county = array()){
        if($county){
            get_instance()->loadModel('MerchantUserModel', 'mMerUser', 'merchant');
            $this->db->join($this->mMerUser->tbShop. ' s', 's.merchant_id = f.merchant_id')
                     ->where_in('s.county', $county);
        }
        $this->db->select('count(if(f.consumer_id >0, f.id, null)) as consumer,
                            count(if(f.merchant_id >0, f.id, null)) as merchant')
                ->where('f.status', 1);

        return $this->getOne($this->tbFeedback. ' f');

    }

    /**
     * 获取状态
     *
     * @param	integer	$key
     * @param	boolean	$show	是否显示
     * @return	mixed
     */
    public function getStatus($key = null, $show = false){

        $config = array(
            1 => '未处理',
            2 => '已处理',
        );

        if($key !== null){

            if(isset($config[$key]) && ! $show){
                return $config[$key];
            }

            if(isset($config[$key]) && $show){

                if($key == 1){
                    return '<span class="label label-danger status">'.$config[$key].'</span>';
                }else if($key == 2){
                    return '<span class="label label-success status">'.$config[$key].'</span>';
                }
            }
            return '';
        }

        return $config;
    }
}